//
//  ViewController.m
//  摇一摇
//
//  Created by boom on 2017/5/3.
//  Copyright © 2017年 boomLi. All rights reserved.
//

#import "ViewController.h"
#import <AudioToolbox/AudioToolbox.h>

@interface ViewController ()

@property (nonatomic, assign) SystemSoundID soundID;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置允许摇一摇功能
    [UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
    
    // 并让自己成为第一响应者
    [self becomeFirstResponder];
}


- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"开始摇动");
    return;
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"取消摇动");
    return;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.subtype == UIEventSubtypeMotionShake) { // 判断是否是摇动结束
        NSLog(@"摇动结束");
        //播放音效
//        SystemSoundID   soundID;  // shake_sound_male.mp3
        
        if (_soundID) {
            AudioServicesDisposeSystemSoundID(_soundID);
        }

        NSString *path = [[NSBundle mainBundle ] pathForResource:@"宋慧乔步步高广告音乐" ofType:@"caf"];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &_soundID);
        AudioServicesPlaySystemSound (_soundID);
        //设置震动
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }  
    return;  
}


@end
